package com.raso.example;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.ollama.OllamaChatModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.output.Response;

/**
 * 普通聊天测试入口
 *
 * @author raos
 * @date 2025/03/17
 */
public class Main {

    public static void main(String[] args) {
        sayOllamaHello2();
        //sayOpenAIHello2();
        //sayOllamaHello();
        //sayOpenAIHello();
    }

    // 测试 Ollama 多轮聊天
    public static void sayOllamaHello2() {
        ChatLanguageModel model = OllamaChatModel.builder()
                .baseUrl("http://localhost:11434")
                .modelName("deepseek-r1:1.5b")
                .build();

        UserMessage userMessage1 = UserMessage.userMessage("我是提问者，你可以称呼我为宇枫");
        Response<AiMessage> response1 = model.generate(userMessage1);
        AiMessage aiMessage1 = response1.content(); // 大模型的第一次响应

        System.out.println(aiMessage1.text());
        System.out.println("--Ollama多轮会话--\n");

        // 下面一行代码是重点
        Response<AiMessage> response2 = model.generate(userMessage1, aiMessage1, UserMessage.userMessage("请问，我叫什么"));
        AiMessage aiMessage2 = response2.content(); // 大模型的第二次响应
        System.out.println(aiMessage2.text());
    }

    // 测试 OpenAI 多轮聊天
    public static void sayOpenAIHello2() {
        ChatLanguageModel model = OpenAiChatModel.builder()
                .baseUrl("http://langchain4j.dev/demo/openai/v1")
                .apiKey("demo")
                .modelName("gpt-4o-mini")
                .build();

        UserMessage userMessage1 = UserMessage.userMessage("你好，我是宇枫");
        Response<AiMessage> response1 = model.generate(userMessage1);
        AiMessage aiMessage1 = response1.content(); // 大模型的第一次响应

        System.out.println(aiMessage1.text());
        System.out.println("--OpenAi多轮会话--\n");

        // 下面一行代码是重点
        Response<AiMessage> response2 = model.generate(userMessage1, aiMessage1, UserMessage.userMessage("我叫什么"));
        AiMessage aiMessage2 = response2.content(); // 大模型的第二次响应
        System.out.println(aiMessage2.text());
    }

    // 简单测试 Ollama 聊天模型
    public static void sayOllamaHello() {
        ChatLanguageModel model = OllamaChatModel.builder()
                .baseUrl("http://localhost:11434")
                .modelName("deepseek-r1:1.5b")
                .build();

        String answer = model.generate("你是谁，你能干什么");
        System.out.println(answer);

        System.out.println("--Ollama--\n");
        System.out.println(model.generate("请复述你是谁"));
    }

    // 简单测试 OpenAI 聊天模型
    public static void sayOpenAIHello() {
        ChatLanguageModel model = OpenAiChatModel.builder()
                .baseUrl("http://langchain4j.dev/demo/openai/v1")
                .apiKey("demo")
                .modelName("gpt-4o-mini")
                .build();

        String answer = model.generate("你是谁，你能干什么");
        System.out.println(answer);

        System.out.println("--OpenAi--\n");
        System.out.println(model.generate("请复述你是谁"));
    }

}